package cn.mrcode.mrweb.aspect;

import cn.mrcode.smartFramework.annotation.Aspect;
import cn.mrcode.smartFramework.annotation.Controller;
import cn.mrcode.smartFramework.proxy.AspectProxy;
import cn.mrcode.smartFramework.utils.PropsUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;

/**
 * Created by zhuqiang on 2015/10/25 0025.
 * 拦截所有的controller方法
 */
@Aspect(Controller.class)
public class ControllerAspect extends AspectProxy{
    private static final Logger LOGGER = LoggerFactory.getLogger(PropsUtil.class);
    private long begin; //开始时间

    @Override
    public void before(Class<?> cls, Method method, Object[] params) throws Throwable {
        begin = System.currentTimeMillis();
        LOGGER.debug(String.format("begin:class:%s,method:%s",cls.getName(),method.getName()));
    }

    @Override
    public void after(Class<?> cls, Method method, Object[] params) throws Throwable {
        long end = System.currentTimeMillis();
        LOGGER.debug(String.format("end:class:%s,method:%s,耗时:%s", cls.getName(), method.getName(),end - begin));
    }

    @Override
    public boolean intercept(Class<?> cls, Method method, Object[] params) throws Throwable {
        if("hashCode".equals(method.getName()) || "toString".equals(method.getName())){
            return false;
        }
        return super.intercept(cls, method, params);
    }
}
